<template>
  <div>
    雪场列表
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>服装列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="25">
        <!-- 搜索区域     -->
        <el-col :span="10">
          <el-input
            placeholder="请输入搜索内容"
            clearable
            @clear="getClothesList"
            v-model="queryInfo.query"
          >
            <template #append>
              <el-button
                icon="el-icon-search"
                @click="getClothesList"
              ></el-button>
            </template>
          </el-input>
        </el-col>
        <el-select v-model="queryInfo.type" placeholder="请选择服装部位">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.label"
            @click="getClothesList"
            ><span style="float: left">{{ item.label }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{
              item.value
            }}</span>
          </el-option>
        </el-select>
        <el-col :span="4">
          <!-- 搜索按钮 -->
          <el-button type="primary" @click="addDialog">添加服装 </el-button>
        </el-col>
      </el-row>

      <!-- 服装列表 border边框，stripe隔行变色 -->
      <el-table :data="clothesList" border stripe>
        <!-- 索引列 -->
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="clothesType" label="服装部位"> </el-table-column>
        <el-table-column prop="clothesName" label="服装名称"> </el-table-column>
        <el-table-column prop="clothesPrice" label="服装价格">
        </el-table-column>
        <el-table-column prop="clothesView" label="服装图片">
          <template v-slot="scope">
            <img width="240" height="200" :src="port + scope.row.clothesView" />
          </template>
        </el-table-column>
        <el-table-column prop="clothesNumber" label="服装数量">
        </el-table-column>

        <el-table-column label="操作">
          <template v-slot="scope">
            <!-- 修改 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="updateVisible(scope.row)"
            ></el-button>
            <!-- 删除 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteClothes(scope.row.clothesId)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页部分 size-change 每页最大变化 current-change 当前最大变化 layout功能组建-->

      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[1, 2, 3, 4, 5, 10]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 新增服装区域 -->
    <el-dialog
      title="添加服装"
      v-model="addDialogVisible"
      width="50"
      @close="addDialogClosed"
    >
      <el-form
        :model="addForm"
        :rules="addFormrules"
        ref="addFormRef"
        label-width="80px"
      >
        <el-form-item label="服装部位" prop="clothesType">
          <el-input v-model="addForm.clothesType"></el-input>
        </el-form-item>
        <el-form-item label="服装名称" prop="clothesName">
          <el-input v-model="addForm.clothesName"></el-input>
        </el-form-item>
        <el-form-item label="服装价格" prop="clothesPrice">
          <el-input v-model="addForm.clothesPrice"></el-input>
        </el-form-item>
        <el-form-item label="服装图片" prop="clothesView">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:9000/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :headers="{ role: role }"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="port + imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="服装数量" prop="clothesNumber">
          <el-input v-model="addForm.clothesNumber"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addClothes">确定</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 修改服装信息 -->
    <el-dialog
      title="修改服装信息"
      v-model="updateDialogVisible"
      width="50"
      @close="updateDialogClosed"
    >
      <el-form
        :model="updateForm"
        :rules="updateFormrules"
        ref="updateFormRef"
        label-width="80px"
      >
        <el-form-item label="服装部位" prop="clothesType">
          <el-input v-model="updateForm.clothesType"></el-input>
        </el-form-item>
        <el-form-item label="服装名称" prop="clothesName">
          <el-input v-model="updateForm.clothesName"></el-input>
        </el-form-item>
        <el-form-item label="服装价格" prop="clothesPrice">
          <el-input v-model="updateForm.clothesPrice"></el-input>
        </el-form-item>
        <el-form-item label="服装图片" prop="clothesView">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:9000/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :headers="{ role: role }"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="port + imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="服装数量" prop="clothesNumber">
          <el-input v-model="updateForm.clothesNumber"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="updateDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="updateClothes">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      //图片上传
      imageUrl: "",
      port: "http://localhost:9000",
      role: "",
      imageName: "",
      //查询信息实体
      queryInfo: {
        query: "", //查询信息
        type: "",
        pageNum: 1, //当前页
        pageSize: 5, //每页的最大值
      },
      clothesList: [], //服装列表
      total: 0, //总记录数
      addDialogVisible: false, //对话框隐藏显示状态
      updateDialogVisible: false,
      //添加表单信息
      addForm: {
        clothesType: "",
        clothesName: "",
        clothesPrice: "",
        clothesView: "",
        clothesNumber: "",
      },
      //更新表单信息
      updateForm: {
        clothesId: 0,
        clothesType: "",
        clothesName: "",
        clothesPrice: "",
        clothesView: "",
        clothesNumber: "",
      },
      //下拉选择服装部位
      options: [
        {
          value: "helmet",
          label: "头盔",
        },
        {
          value: "goggles",
          label: "护目镜",
        },
        {
          value: "coat",
          label: "上衣",
        },
        {
          value: "trousers",
          label: "裤子",
        },
        {
          value: "boots",
          label: "靴子",
        },
      ],
      value: "",
      //表单验证
      addFormrules: {
        clothesType: [
          { required: true, message: "请输入服装部位", trigger: "blur" },

        ],
        clothesName: [
          { required: true, message: "请输入服装名称", trigger: "blur" },
          
        ],
        clothesPrice: [
          { required: true, message: "请输入服装价格", trigger: "blur" },
          
        ],
        clothesView: [
          { required: true, message: "请输入服装图片", trigger: "blur" },
          
        ],
        clothesNumber: [
          { required: true, message: "请输入服装数量", trigger: "blur" },
          
        ],
      },
      //修改表单验证
      updateFormrules: {
        clothesType: [
          { required: true, message: "请输入服装部位", trigger: "blur" },
          
        ],
        clothesName: [
          { required: true, message: "请输入服装名称", trigger: "blur" },
        
        ],
        clothesPrice: [
          { required: true, message: "请输入服装价格", trigger: "blur" },
        
        ],
        clothesView: [
          { required: false, message: "请输入服装图片", trigger: "blur" },
        
        ],
        clothesNumber: [
          { required: true, message: "请输入服装数量", trigger: "blur" },
        
        ],
      },
    };
  },
  created() {
    this.getClothesList();
    this.role = encodeURIComponent(sessionStorage.getItem("role"));
  },
  methods: {
    //获取全部服装信息
    async getClothesList() {
      //调用post
      const { data: res } = await this.$http.get("allClothes", {
        params: this.queryInfo,
      });
      this.clothesList = res.data; //将返回数据赋值
      console.log(res.data);
      this.total = res.numbers;
    },
    //最大数
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getClothesList();
    },
    //pageNum的触发动作
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getClothesList();
    },

    //监听添加服装的操作
    addDialogClosed() {
      this.imageUrl = "";
      this.$refs.addFormRef.resetFields();
    },
    updateDialogClosed() {
      this.imageUrl = "";
      this.$refs.updateFormRef.resetFields();
    },

    addClothes() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post("addClothes", this.addForm);
        if (res != "success") return this.$message.error("操作失败");
        this.$message.success("操作成功");
        this.addDialogVisible = false;
        this.getClothesList();
      });
    },
    addDialog() {
      this.addForm.clothesView = "";
      this.addDialogVisible = true;
    },
    //根据主键删除服装信息
    async deleteClothes(clothesId) {
      console.log(clothesId);
      const confirmResult = await this.$confirm(
        "此操作将永久删除服装，是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmResult != "confirm") {
        return this.$message.info("已取消删除");
      }
      const { data: res } = await this.$http.delete(
        "deleteClothes?clothesId=" + clothesId
      );
      if (res.data != "success") {
        return this.$message.error("删除失败");
      }
      this.$message.success("删除成功");
      this.getClothesList();
    },
    updateVisible(clothesInfo) {
      (this.updateDialogVisible = true),
        (this.updateForm.clothesType = clothesInfo.clothesType),
        (this.updateForm.clothesName = clothesInfo.clothesName),
        (this.updateForm.clothesPrice = clothesInfo.clothesPrice),
        (this.imageUrl = clothesInfo.clothesView),
        (this.updateForm.clothesNumber = clothesInfo.clothesNumber),
        (this.updateForm.clothesId = clothesInfo.clothesId);
      this.updateForm.clothesView = clothesInfo.clothesView;
    },
    async updateClothes() {
      this.$refs.updateFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post(
          "updateClothes",
          this.updateForm
        );
        if (res != "success") return this.$message.error("操作失败");
        this.$message.success("操作成功");
        this.updateDialogVisible = false;
        this.getClothesList();
      });
    },
    handleSave() {
      // 向后端发送 数据
      this.pojo.img = this.imageUrl;
      manager.update(this.id, this.pojo).then((res) => {
        // 修改 或者新增成功
        this.$message({
          message: res.msg,
          type: res.code === 20000 ? "success" : "error",
        });
        if (res.code === 20000) {
          this.fetchData();
        }
        this.dialogFormVisible = false;
      });
    },
    handleAvatarSuccess(res, file) {
      console.log(res);
      this.imageUrl = res.filename;
      this.addForm.clothesView = this.imageUrl;
      console.log(this.imageUrl);
      this.updateForm.clothesView = this.imageUrl;
      // console.log(file.raw)
      // this.imageUrl = URL.createObjectURL(file.raw)
      // 这句话绑定的是node返回的图片给你，应该发挥服务器给的图片地址
      // 保存提交时候还需要pojo.image=this.imageUrl这样就可以吧图片保存到服务器数据库里面
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt10M = file.size / 1024 / 1024 < 10;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt10M) {
        this.$message.error("上传头像图片大小不能超过 10MB!");
      }
      return isJPG && isLt10M;
    },
  },
};
</script>
<style lang="less" scoped>
.el-breadcrumb {
  margin-bottom: 15px;
  font-size: 17px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 300px;
  height: 200px;
  display: block;
}
.form-item {
  margin-left: 10px;
  text-align: center;
  vertical-align: middle;
  margin-bottom: 22px;
  margin-top: 22px;
}
.topbutton {
  margin-top: 22px;
}
</style>
